<?php session_start(); require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=transaksimutasi";

/* Validasi */
$nopol = $_POST['txtnopolisi'];

mysql_select_db($database_con_gl, $con_gl);
	$query_area = "select * from gl_masterkendaraan where nopolisi = '$nopol'";
	$cek      = mysql_query($query_area, $con_gl);
	$row_cek   = mysql_fetch_assoc($cek);
	
if (trim($_POST['nopolisi']) == '') {
	$error[] = '- No. Polisi harus dipilih !!!';
}

if ($row_cek[area] == '') {
	
	print_r($query_data);
	$error[] = '- Area harus dipilih !!!';
	
}
if ($_POST['area'] == '') {
	$error[] = '- Area Mutasi Tujuan harus dipilih !!!';
	
}
if ($_POST['cabang'] == '') {
	$error[] = '- Cabang Mutasi Tujuan harus dipilih !!!';
	
}
/*End validasi */
if (isset($error)) {
	echo "<b>Error</b>: <br />".implode("<br />", $error);
}else{
	if($act =='add') {
	$today = date("Y-m-d"); 
	
	$query = "INSERT INTO gl_transaksimutasi (id, tgltransaksi, tglefektif, nopolisi, areaasal,cabangasal,kearea,kecabang, keterangan,created_by,created_at) VALUES (NULL,'$_POST[tgltransaksi]','$_POST[tglefektif]','$_POST[nopolisi]','$row_cek[area]','$row_cek[cabang]','$_POST[area]','$_POST[cabang]','$_POST[keterangan]','$_SESSION[nama]','$today')";
	}else if($act=='edit'){
	$query ="UPDATE gl_transaksimutasi SET nopolisi='$_POST[nopolisi]', areaasal = '$row_cek[area]',cabangasal = '$row_cek[cabang]',kearea='$_POST[area]',kecabang='$_POST[cabang]', tgltransaksi='$_POST[tgltransaksi]', tglefektif='$_POST[tglefektif]', keterangan='$_POST[keterangan]',updated_by ='$_SESSION[nama]' WHERE id='$_POST[id]'";
	
	}else{
	    $query = "delete from gl_transaksimutasi where id='$_POST[id]'";
	}
	$addquery = "UPDATE gl_masterkendaraan SET area='$_POST[area]',cabang='$_POST[cabang]' where nopolisi = '$_POST[txtnopolisi]'";
		mysql_query($addquery, $con_gl) or die (mysql_error());
		
	mysql_select_db($database_con_gl, $con_gl);
	$runquery = mysql_query($query, $con_gl);
	if($runquery&& $addquery) {
		echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil ";if($act=='add' OR $act=='edit') { echo "disimpan";}else{ echo "dihapus"; } echo " ...";
	}else{
		echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
	}
}
?>
<?php if($runquery) { ?><script type="text/javascript">setTimeout("location.href='<?php echo $url;?>'", 2000);</script><?php } ?>